import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';

class RxDartDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    // https://www.jianshu.com/p/e0b0169a742e
    // https://blog.csdn.net/yumi0629/article/details/82776030
    var items = {
      '在后台看输出结果': () {},
      'Stream': () {
        Stream.fromIterable([1, 5, 7])
            .interval(Duration(seconds: 2))
            .listen(print);

        Stream.periodic(Duration(seconds: 1), (x) => x * x)
            .take(10)
            .listen(print);

        Stream.fromFuture(
                Future.delayed(Duration(seconds: 3), () => 'Hello Future!'))
            .listen(print);

        Stream.fromIterable([1, 2, 7]).map((i) => i / 3).listen(print);
      },
      'Subjects -> PublishSubjects': () {
        var subjects = PublishSubject<String>();
        subjects.listen((item) => print(item));
        subjects.add('item 1');
        subjects.add('item 2');

        subjects.listen((item) => print(item.toUpperCase()));
        subjects.add('item 3');
        subjects.add('item 5');

        subjects.close();
      },
      'Subjects -> BehaviourSubject': () {
        var subject = BehaviorSubject<String>();
        subject.listen((item) => print(item));

        subject.add("Item1");
        subject.add("Item2");

        subject.listen((item) => print(item.toUpperCase()));

        subject.add("Item3");

        subject.close();
      },
      'Subjects -> ReplaySubject': () {
        var subject = ReplaySubject<String>();
        subject.listen((item) => print(item));

        subject.add("Item1");
        subject.add("Item2");

        subject.listen((item) => print(item.toUpperCase()));

        subject.add("Item3");

        subject.close();
      },
      'Rx 执行多个流': () {
        var obs = Rx.merge([
          Stream.fromIterable([1, 2, 3]),
          Stream.fromIterable([4, 5, 6]),
          Stream.fromIterable([7, 8, 9]),
        ]);
        obs.listen((i) => print(i));
      },
      'Rx 顺序执行多个流': () {
        var obs = Rx.concat([
          Stream.fromIterable([1, 2, 3]),
          Stream.fromIterable([4, 5, 6]),
          Stream.fromIterable([7, 8, 9]),
        ]);
        obs.listen(print);
      },
      'Stream every 检查每个item是否符合要求': () {
        var s = Stream.fromIterable([1, 2, 3]);
        s.every((i) => i < 10).then(print);
        // s.listen(print);
      },
      'Stream asBroadcastStream 多订阅流': () {
        var s = Stream.fromIterable([1, 2, 3]).asBroadcastStream();
        s.every((i) => i < 10).then(print);
        s.listen(print);
      },
      'tip: Stream的方法PublishSubjects、BehaviourSubject、ReplaySubject也可以用，因为他们是继承至Stream':
          () {},
    };

    return Scaffold(
      appBar: AppBar(
        title: Text('RxDartDemo'),
      ),
      body: ListView.separated(
        itemBuilder: (context, position) {
          var item = items.entries.elementAt(position);
          return ListTile(
            title: Text(item.key),
            onTap: item.value,
          );
        },
        itemCount: items.length,
        separatorBuilder: (context, position) => Divider(
          height: 1,
        ),
      ),
    );
  }
}
